#include "database_op.h"

int insert_to_userTable(const char *username, const char *salt, const char *cryptpasswd, const char *pwd)
{
    int return_flag = 0;
    MYSQL *conn;
    // MYSQL_RES *res;
    // MYSQL_ROW row;
    const char *server = "localhost";
    const char *user = "root";
    const char *password = "123";
    const char *database = "Asuka";
    char query[200] = {0};
    sprintf(query, "insert into userTable(username, salt, cryptpasswd, pwd) values('%s','%s', '%s', '%s')",
            username, salt, cryptpasswd, pwd);
#ifdef DEBUG
    puts(query);
#endif
    int t;
    conn = mysql_init(NULL);
    if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
    {
        printf("Error connecting to database:%s\n", mysql_error(conn));
        return_flag = -1;
    }
    else
    {
        printf("Connected...\n");
    }
    t = mysql_query(conn, query); //执行sql
    if (t)
    {
        printf("Error making query:%s\n", mysql_error(conn));
        return_flag = -1;
    }
    else
    {
        printf("insert success,insert row=%ld\n", (long)mysql_affected_rows(conn));
    }
    mysql_close(conn);
    return return_flag;
}

int select_to_login(const char *username, char *salt, char *cryptpasswd, int *user_id)
{
    int return_flag = 0;
    MYSQL *conn;    //连接
    MYSQL_RES *res; //结果操作指针
    MYSQL_ROW row;  //一行的数据是存在row中间
    const char *server = "localhost";
    const char *user = "root";
    const char *password = "123";
    const char *database = "Asuka"; //要访问的数据库名称
    char query[300] = "select * from userTable where username = '";
    sprintf(query, "%s%s%s", query, username, "'");
#ifdef DEBUG
    puts(query);
#endif
    unsigned int t;
    conn = mysql_init(NULL); //要初始化
    if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
    {
        printf("Error connecting to database:%s\n", mysql_error(conn));
        return_flag = -1;
    }
    else
    {
        printf("Connected...\n");
    }

    t = mysql_query(conn, query); //执行sql语句
    if (t)
    {
        printf("Error making query:%s\n", mysql_error(conn)); // sql的语法出错
        return_flag = -1;
    }
    else
    {
        res = mysql_use_result(conn); //获取结果
        char result[200] = {0};
        if (res)
        {
            if ((row = mysql_fetch_row(res)) != NULL)
            {
                *user_id = atoi(row[0]);
                strcpy(salt, row[2]);
                strcpy(cryptpasswd, row[3]);
                return_flag = 0;
            }
            else
            {
                return_flag = -1;
            }
            
        }
        else
        {
            return_flag = -1;
        }
        mysql_free_result(res);
    }
    mysql_close(conn);
    return return_flag;
}

int select_username_by_userid(char *username, int user_id)
{
    int return_flag = 0;
    MYSQL *conn;    //连接
    MYSQL_RES *res; //结果操作指针
    MYSQL_ROW row;  //一行的数据是存在row中间
    const char *server = "localhost";
    const char *user = "root";
    const char *password = "123";
    const char *database = "Asuka"; //要访问的数据库名称
    char query[300] = "select * from userTable where id = ";
    sprintf(query, "%s%d", query, user_id);
#ifdef DEBUG
    puts(query);
#endif
    unsigned int t;
    conn = mysql_init(NULL); //要初始化
    if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
    {
        printf("Error connecting to database:%s\n", mysql_error(conn));
        return_flag = -1;
    }
    else
    {
        printf("Connected...\n");
    }

    t = mysql_query(conn, query); //执行sql语句
    if (t)
    {
        printf("Error making query:%s\n", mysql_error(conn)); // sql的语法出错
        return_flag = -1;
    }
    else
    {
        res = mysql_use_result(conn); //获取结果
        char result[200] = {0};
        if (res)
        {
            while ((row = mysql_fetch_row(res)) != NULL)
            {
                strcpy(username, row[1]);
            }
            return_flag = 0;
        }
        else
        {
            return_flag = -1;
        }
        mysql_free_result(res);
    }
    mysql_close(conn);
    return return_flag;
}

int select_allfile_by_userid(char *filelist, int user_id, const int curdir_code)
{
    int return_flag = 0;
    MYSQL *conn;                      //连接
    MYSQL_RES *res;                   //结果操作指针
    MYSQL_ROW row;                    //一行的数据是存在row中间
    const char *server = "localhost"; // 数据库地址
    const char *user = "root";        // 用户名
    const char *password = "123";     // 密码
    const char *database = "Asuka";   //要访问的数据库名称
    char query[300] = "";
    sprintf(query, "select * from virtualFileTable where owner_id = %d \
                and parent_code = %d",
            user_id, curdir_code);

#ifdef DEBUG
    puts(query);
#endif
    unsigned int t;
    conn = mysql_init(NULL); //要初始化
    if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
    {
        printf("Error connecting to database:%s\n", mysql_error(conn));
        return_flag = -1;
    }
    else
    {
        printf("Connected...\n");
    }

    t = mysql_query(conn, query); //执行sql语句
    if (t)
    {
        printf("Error making query:%s\n", mysql_error(conn)); // sql的语法出错
        return_flag = -1;
    }
    else
    {
        res = mysql_use_result(conn); //获取结果
        char each_row[200] = {0};
        if (res)
        {
            while ((row = mysql_fetch_row(res)) != NULL)
            {
                memset(each_row, 0, sizeof(each_row));
                sprintf(each_row, "%-3s %-20s %-10s\n", row[6], row[2], row[5]);
                strcat(filelist, each_row);
            }
            return_flag = 0;
        }
        else
        {
            return_flag = -1;
        }
        mysql_free_result(res);
    }
    mysql_close(conn);
    return return_flag;
}

int select_dir_by_dirname(const char *filename, int user_id, int *parent_code)
{
    int return_flag = 0;
    MYSQL *conn;                      //连接
    MYSQL_RES *res;                   //结果操作指针
    MYSQL_ROW row;                    //一行的数据是存在row中间
    const char *server = "localhost"; // 数据库地址
    const char *user = "root";        // 用户名
    const char *password = "123";     // 密码
    const char *database = "Asuka";   //要访问的数据库名称
    char query[300] = {0};

    if (!strcmp(filename, ".."))
    {
        strcpy(query, "select * from virtualFileTable where id = (select parent_code from virtualFileTable where owner_id = ");
        sprintf(query, "%s%d%s%d%s%s%s", query, user_id, " and parent_code = ", *parent_code, " and filename = '", filename, "')");
    }
    else
    {
        strcpy(query, "select * from virtualFileTable where owner_id = ");
        sprintf(query, "%s%d%s%s%s%s", query, user_id, " and filename = ", "'", filename, "'");
    }

#ifdef DEBUG
    puts(query);
#endif
    unsigned int t;
    conn = mysql_init(NULL); //要初始化
    if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
    {
        printf("Error connecting to database:%s\n", mysql_error(conn));
        return_flag = -1;
    }
    else
    {
        printf("Connected...\n");
    }

    t = mysql_query(conn, query); //执行sql语句
    if (t)
    {
        printf("Error making query:%s\n", mysql_error(conn)); // sql的语法出错
        return_flag = -1;
    }
    else
    {
        res = mysql_use_result(conn); //获取结果
        if (res)
        {
            if ((row = mysql_fetch_row(res)) != NULL)
            {
                if (!strcmp(filename, ".."))
                {
                    *parent_code = atoi(row[1]);
                    // select_parentcode_by_dirname();
                    return_flag = 0;
                }
                else
                {
                    if (!strcmp(row[6], "d"))
                    {
                        *parent_code = atoi(row[0]);
                        printf("parent_code = %d\n", atoi(row[0]));
                        return_flag = 0;
                    }                                       
                    else
                    {
                        return_flag = -1;
                    }
                }
            }
            else
            {
                return_flag = -1;
            }
        }
        else
        {
            return_flag = -1;
        }
        mysql_free_result(res);
    }
    mysql_close(conn);
    return return_flag;
}

int delete_file_by_filename(const char *filename, int user_id, int parent_code)
{
    int return_flag = 0;
    MYSQL *conn;
    const char *server = "localhost";
    const char *user = "root";
    const char *password = "123";
    const char *database = "Asuka";
    char query[200] = "delete from virtualFileTable where filename = '";
    sprintf(query, "%s%s%s%d",query, filename, "' and owner_id = ", user_id);
#ifdef DEBUG
    puts(query);
#endif

    char filetype[10] = {0};
    int ret = select_fileinfo_by_filename(filename, parent_code, filetype);
    if (!strcmp(filetype, "d") || ret == -1)
    {
        printf("delete faield\n");
        return -1;
    }

    int t;
    conn = mysql_init(NULL);
    if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
    {
        printf("Error connecting to database:%s\n", mysql_error(conn));
        return_flag = -1;
    }
    else
    {
        printf("Connected...\n");
    }
    t = mysql_query(conn, query);
    if (t)
    {
        printf("Error making query:%s\n", mysql_error(conn));
        return_flag = -1;
    }
    else
    {
        if ((long)mysql_affected_rows(conn) == 1)
        {
            printf("delete success,delete row=%ld\n", (long)mysql_affected_rows(conn));
        }
        else
        {
            return_flag = -1;
        }
    }
    mysql_close(conn);
    return 0;
}

int select_fileinfo_by_filename(const char *filename, int parent_code, char *filetype)
{
    int return_flag = 0;
    MYSQL *conn;                      //连接
    MYSQL_RES *res;                   //结果操作指针
    MYSQL_ROW row;                    //一行的数据是存在row中间
    const char *server = "localhost"; // 数据库地址
    const char *user = "root";        // 用户名
    const char *password = "123";     // 密码
    const char *database = "Asuka";   // 要访问的数据库名称
    char query[300] = "";
    sprintf(query, "select * from virtualFileTable where filename = '%s' and parent_code = %d", filename, parent_code);

#ifdef DEBUG
    puts(query);
#endif
    unsigned int t;
    conn = mysql_init(NULL); //要初始化
    if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
    {
        printf("Error connecting to database:%s\n", mysql_error(conn));
        return_flag = -1;
    }
    else
    {
        printf("Connected...\n");
    }

    t = mysql_query(conn, query); //执行sql语句
    if (t)
    {
        printf("Error making query:%s\n", mysql_error(conn)); // sql的语法出错
        return_flag = -1;
    }
    else
    {
        res = mysql_use_result(conn); //获取结果
        char each_row[200] = {0};
        if (res)
        {
            if ((row = mysql_fetch_row(res)) != NULL)
            {
                strcpy(filetype, row[6]);
                return_flag = 0;
            }
            else
            {
                return_flag = -1;
            }
            
        }
        else
        {
            return_flag = -1;
        }
        mysql_free_result(res);
    }
    mysql_close(conn);
    return return_flag;
}

int insert_to_virualFileTable(const char *filename, const char *md5_str, const int filesize)
{
    int return_flag = 0;
    MYSQL *conn;
    // MYSQL_RES *res;
    // MYSQL_ROW row;
    const char *server = "localhost";
    const char *user = "root";
    const char *password = "123";
    const char *database = "Asuka";
    char query[200] = {0};
    // insert into virtualFileTable (parent_code, filename, owner_id, md5, filesize, type) values (21, '..', 4062, '5z82zu', '2K', 'd');
    sprintf(query, "insert into virtualFileTable(parent_code, filename, owner_id, md5, filesize, type) \
                    values('%d','%s','%d','%s', %d, '%s')",
                    cur_client.parent_code, filename, cur_client.user_id, md5_str, filesize, "f");
#ifdef DEBUG
    puts(query);
#endif
    int t;
    conn = mysql_init(NULL);
    if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
    {
        printf("Error connecting to database:%s\n", mysql_error(conn));
        return_flag = -1;
    }
    else
    {
        printf("Connected...\n");
    }
    t = mysql_query(conn, query); //执行sql
    if (t)
    {
        printf("Error making query:%s\n", mysql_error(conn));
        return_flag = -1;
    }
    else
    {
        printf("insert success,insert row=%ld\n", (long)mysql_affected_rows(conn));
    }
    mysql_close(conn);
    return return_flag;
}